package com.lxm.enigma.algorithm;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: Lys
 * @Date 2022/5/31
 * @Describe
 **/
class Md5Utils {

    private static final Pattern pattern = Pattern.compile("[A-Z]");

    protected static Map<String, String> obj2Map(Object obj, boolean toUnderline) throws Exception {
        Map<String, String> map = new HashMap<>();
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter != null ? getter.invoke(obj) : null;
            if (null != value) {
                String valuestr = String.valueOf(value);
                if (toUnderline){
                    key = underline(key);
                }
                map.put(key, valuestr);
            }
        }
        return map;
    }

    /**
     * 驼峰转下划线
     *
     * @param str
     * @return
     */
    protected static String underline(String str) {
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer(str);
        if (matcher.find()) {
            sb = new StringBuffer();
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
            matcher.appendTail(sb);
        } else {
            return sb.toString();
        }
        return underline(sb.toString());
    }

    /**
     * 字典排序，然后组装成url带参形式的字符串
     *
     * @param arrs
     * @return
     */
    protected static String sort(String[] arrs) {
        Arrays.sort(arrs);
        StringBuilder sb = new StringBuilder();
        int length = arrs.length;
        for (int i = 0; i < length; i++) {
            var one = arrs[i];
            if (i != length - 1) {
                sb.append(one).append("&");
            } else {
                sb.append(one);
            }
        }
        return sb.toString();
    }

    protected static String[] map2Arr(Map<String, String> map) {
        String[] arrs = new String[map.size()];
        List<String> strs = new ArrayList<>();
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            String temp = entry.getKey() + "=" + entry.getValue();
            strs.add(temp);
        }
        strs.toArray(arrs);
        return arrs;
    }
}
